package org.agent.aigccore.model.bo;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.agent.aigccore.basic.enums.IotDeviceBusinessGroupStatusEnum;
import org.agent.aigccore.basic.serializable.BaseExtInfoBaseDBModel;
import org.agent.aigccore.basic.serializable.CustomConverter;
import org.agent.aigccore.model.po.IotDeviceBusinessGroupPO;
import org.agent.aigccore.model.vo.IotDeviceBusinessGroupVO;
import org.mapstruct.DecoratedWith;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import org.springframework.core.codec.AbstractDecoder;

import java.util.Arrays;
import java.util.List;

/**
 * IoT设备业务组BO类
 */
@Slf4j
@Setter
@Getter
@Schema(description = "IoT设备业务组业务对象")
public class IotDeviceBusinessGroupBO extends BaseExtInfoBaseDBModel {

    private String businessGroupName;
    private IotDeviceBusinessGroupStatusEnum status;
    private List<String> longingDeviceIds;
    private List<String> unloadingDeviceIds;
    private String creator;
    private String modifier;

    /**
     * 转换类
     */
    public static final Convert convert = Mappers.getMapper(Convert.class);

    @Mapper(uses = CustomConverter.class)
    @DecoratedWith(AbstractDecoder.class)
    public interface Convert {

        @Mappings({
                @Mapping(source = "longingDeviceIds", target = "longingDeviceIds", qualifiedByName = "listToString"),
                @Mapping(source = "unloadingDeviceIds", target = "unloadingDeviceIds", qualifiedByName = "listToString")
        })
        IotDeviceBusinessGroupPO toPO(IotDeviceBusinessGroupBO po);

        @Mappings({
                @Mapping(source = "longingDeviceIds", target = "longingDeviceIds", qualifiedByName = "stringToList"),
                @Mapping(source = "unloadingDeviceIds", target = "unloadingDeviceIds", qualifiedByName = "stringToList")
        })
        IotDeviceBusinessGroupBO formPO(IotDeviceBusinessGroupPO po);

        IotDeviceBusinessGroupVO toVO(IotDeviceBusinessGroupBO bo);

        IotDeviceBusinessGroupBO fromVO(IotDeviceBusinessGroupVO vo);
    }

    /**
     * 抽象转换器
     */
    public static abstract class AbstractDecoder implements Convert {
        private final Convert convert;

        public AbstractDecoder(Convert convert) {
            this.convert = convert;
        }
    }

    @org.mapstruct.Named("listToString")
    static String listToString(List<String> list) {
        return list != null ? String.join(",", list) : null;
    }

    @org.mapstruct.Named("stringToList")
    static List<String> stringToList(String str) {
        return str != null ? Arrays.asList(str.split(",")) : null;
    }
}